import { areaList } from '@vant/area-data';
import {request} from "../../utils/request"
import Toast from '@vant/weapp/toast/toast';
import dayjs from "dayjs"
Page({
  data: {
    areaList,
    showPosition:false,
    showTime:false,
    minHour: 8,
    maxHour: 22,
    minDate: new Date().getTime(),
    maxDate: new Date(2023, 12, 30).getTime(),
    currentDate: new Date().getTime(),
    formatter(type:any, value:any) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      if (type === 'hour') {
        return `${value}时`;
      }
      if (type === 'minute') {
        return `${value}分`;
      }
      return value;
    },
    showPay: false,
    list:[1,2,3,4,5,6,7,8,9],
    password:<Array<number>>[],
    info:{
      user:"",
      type:2,
      price:99,
      userName:"",
      number:"",
      city:"",
      time:""
    }
  },
  selectPosition(){
    this.setData({
      showPosition:true
    })
  },
  positionClose(){
    this.setData({
      showPosition:false
    })
  },
  positionConfirm(e:any){
    let position = ""
    e.detail.values.forEach((item:any)=>{
      position = position + ' ' + item.name
    })
    this.setData({
      "info.city":position,
      showPosition:false
    })
  },
  positionCancel(){
    this.setData({
      showPosition:false
    })
  },
  selectTime(){
    this.setData({
      showTime:true
    })
  },
  timeClose(){
    this.setData({
      showTime:false
    })
  },
  timeConfrim(e:any){
    let time = dayjs(e.detail).format('YYYY-MM-DD HH:MM')
    this.setData({
      "info.time":time,
      showTime:false
    })  
  },
  pay() {
    Toast.loading({
      message: '支付中...',
      duration: 1000,
      forbidClick: true,
      loadingType: 'spinner',
      onClose:()=>{
        this.setData({
          showPay:true
        })
      }
    })
},
  infoChange(e:any){
    let name = e.currentTarget.dataset.name
    if(name=="userName"){
      this.setData({
        "info.userName":e.detail
      })
    }
    if(name=='number'){
      this.setData({
        "info.number":e.detail
      })
    }
  },
  closePopup() {
  this.setData({ showPay: false });
  },
  async getPassword(e: any){
  if(this.data.password.length==5){
    this.data.password.push(e.currentTarget.dataset.number)
    this.setData({password: this.data.password,showPay:false})
    try {
      let info = this.data.info
      let {id} = wx.getStorageSync("userInfo")
      info.user = id
      const result = await request("/order","POST",{
        ...info
      })
      console.log(result);
      Toast.success({
        type: 'success',
        message: '提交成功',
        duration:1000,
        onClose: async() => {
          this.setData({password:[]})
          wx.reLaunch({url:"/pages/order/order"})
        },
      })
    } catch (error) {
      Toast.success({
        type: 'fail',
        message: '提交失败',
        duration:1000,
        onClose: async() => {
          this.setData({password:[]})
        },
      })
    }
  }else if(this.data.password.length<5){
    this.data.password.push(e.currentTarget.dataset.number)
    this.setData({password: this.data.password})
  }else{
    return
  }
  },
  removePassword(){
    this.data.password.pop()
    this.setData({password: this.data.password})
  }, 
})